package com.kooison.trafficMonitor;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.kooison.crawler.xml.impl.TdCrawler;
import com.kooison.crawler.xml.jaxb.model.td.Area;
import com.kooison.crawler.xml.jaxb.model.td.Dot;
import com.kooison.crawler.xml.jaxb.model.td.TrafficInfo;
import com.kooison.util.properties.PropertyLoader;

public class XmlMonitorController extends AbstractController {

	private static TdCrawler crawler;
	
	public TdCrawler getCrawler() {
		return crawler;
	}

	public void setCrawler(TdCrawler crawler) {
		this.crawler = crawler;
	}

	private static Properties props;
	
	private String view;
	
	public void setView(String view) {
		this.view = view;
	}
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (props == null)
			props = PropertyLoader.loadProperties("location.properties");
		
		String location = ServletRequestUtils.getStringParameter(request, "location", "");
		boolean reload = ServletRequestUtils.getBooleanParameter(request, "reload", false);
		String imgSize = ServletRequestUtils.getStringParameter(request, "imgSize", "small");
		
		// TdCrawler crawler = new TdCrawler();
		if ("".equals(location)) {
			crawler.setAreas(new String[]{ "" });
		} else {
			// String[] locations = location.split(",");
			crawler.setAreas(location.split(","));
		}

		crawler.setPath("http://www.news.gov.hk/common/feedin/traffic/publish_area/trafficHK.xml");
		crawler.setMarshallType(TrafficInfo.class);
		
			TrafficInfo ti = (TrafficInfo) crawler.getResult();
			logger.info(ti.toString());
			
			/*
			PersistenceManager pm = PMF.get().getPersistenceManager();
			ArrayList<Camera> l = new ArrayList<Camera>();
			*/
			for (Area a : ti.getArea()) {
				for (Dot d :  a.getDot()) {
					// Camera c = new Camera(d.getDotID(), "", "");
					// l.add(c);
					String latlng = props.getProperty("latlng." + d.getDotID());
					if (latlng != null) {
						d.setDotLatlng(latlng);
					}
				}
			}
			/*
			pm.makePersistentAll(l);
			*/
		
		
		/*
		TrafficInfo trafficInfo = null;
		
		if (crawler == null) {
			crawler = new XmlCrawler();
		}
		*/
		

		/*
		if ("".equals(location)) {
			trafficInfo = crawler.getTrafficInfo();
		} else {
			// String[] locations = location.split(",");
			trafficInfo = crawler.getTrafficInfo(location);
		}
		*/
		/*
		if (reload) {
			crawler.reload();
		}
		
		trafficInfo = crawler.getTrafficInfo(location);
		*/
		
		Map map = new HashMap();
		// map.put("trafficInfo", trafficInfo);
		map.put("trafficInfo", ti);
		map.put("location", location);
		map.put("reload", reload);
		map.put("imgSize", imgSize);
		
		// String view = ServletRequestUtils.getStringParameter(request, "view", "");
		if (view.equals("map")) {
			return new ModelAndView("showTrafficInMap", map);
		// } else if (view.equals("debug")) {
		// 	return new ModelAndView("debug2", map);
		} else {
			return new ModelAndView("showTraffic2", map);
		}
	}
}
